<?php


namespace app\project\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\project\model\ProjectUuid as ModelProjectUuid;

/**
 * 项目号管理类
 * Class ProjectUuid
 * @package app\project\admin
 */
class ProjectUuid extends Admin
{
    
    public function initialize()
    {
        parent::initialize();
        $this->model = new ModelProjectUuid();
    }

    public function index()
    {
        $data = $this->model->select();
        $btn_dels =
            [
                'title'       => '删除',
                'icon'        => 'fa fa-times-circle-o',
                'class'       => 'btn btn-danger ajax-post confirm',
                'target-form' => 'ids',
                'href'  => url('del'),
                'data-title'=>'真的要删除吗?'
            ];
        $btn_del = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('del', ['ids' => '__id__']),
            'data-title'=>'真的要删除吗?'
        ];

        return ZBuilder::make('table')
            ->addColumns([
                ['project_uuid', '项目号'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],true)
            ->addTopButton('del',$btn_dels)
            ->setRowList($data)
            ->addRightButton('del',$btn_del)
            ->fetch();
    }

    public function add()
    {
        if($this->request->isPost()) {
            $data = $this->request->post('', null, 'trim');
            $result = $this->validate($data, 'ProjectUuid');
            if (true !== $result) {
                $this->error($result);
            }
            $data['create_time'] = time();
            if ($info = $this->model->create($data)){
                $details = UID . '操作projectUuid_add,id为' . $info['id'];
                action_log('projectUuid_add', 'project_uuid', $info['id'], UID, $details);
                $this->success('新增成功','index',['_parent_reload'=>1,'_close_pop'=>1]);
            }
            $this->error('新增失败');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'project_uuid', '项目号','请输入项目号'],
            ])
            ->fetch();
    }

    public function del($ids=0)
    {
        if ($ids == 0){
            $ids = $this->request->post('ids', null, 'trim');
        }
        $this->model->destroy(['id'=>$ids]);
        $this->success('删除成功');
    }
}